<?php 
	error_reporting(E_WARNING);
	include_once('../includes/metodos.php');
	include_once('../header.php');
	connectSql();
?>
<form action="index.php" method="post">
<span>Agenda de </span>
<SELECT name="podo">
	<?php 
	$sql = mysql_query("SELECT * FROM tb_funcionario");
	while ($row = mysql_fetch_array($sql)){
		echo '<OPTION value="'.$row['id'].'">'.$row['nome'].' '.$row['sobrenome'].'</OPTION>';
	}
	?>
</SELECT>
<input type="hidden" name="cmd" value="agenda">
Data: <input type="text" class="datepicker" name="data">
<input type="submit" value="Buscar">
</form>
<hr>
	
	<?php 
	if(isset($_POST['cmd']) || $_POST['cmd']=='agenda'){
		$query = mysql_query("SELECT * FROM tb_cliente");

		$c = 1;
		$hora = array(1=>'08:00',2=>'08:40', 3=>'09:20',4=>'10:00',5=>'10:40', 6=>'11:20',7=>'12:00', 8=>'12:40', 9=>'13:20', 10=>'14:00', 11=>'14:40'
						, 12=>'15:20', 13=>'16:00', 14=>'16:40', 15=>'17:20', 16=>'18:00');

		if(empty($_POST['data'])){
			$data=date("Y-m-d");
		}else{
			$data = $_POST['data'];
		}
		//Criando os horarios
		echo '<table>';
		while ($c<17){
			echo '<tr><td width="50px"><strong>'.$hora[$c].'</strong></td>';
			$agenda = mysql_query("SELECT * FROM tb_agenda a, tb_cliente c WHERE a.hora='".$hora[$c].":00' AND a.data='".$data."' AND a.id_cliente=c.id AND a.id_funcionario=".$_POST['podo']. " ORDER BY a.hora LIMIT 1");
			$num = mysql_num_rows($agenda);
			if($num>0){
			$row = mysql_fetch_array($agenda);
			echo '<td>'.utf8_encode($row['nome']).' '.utf8_encode($row['sobrenome']).'</td></tr>';
			}else{
				echo '<td><form action="new.php" method="post">
						<input type="hidden" name="hora" value="'.$hora[$c].'"/>
						<input type="submit" value="Marcar">
						<input type="hidden" name="podo" value="'.$_POST['podo'].'"/>
						<input type="hidden" name="data" value="'.$_POST['data'].'"/>
					</form></td>';
			}
			++$c;
		}
		echo '</table>';
	}
?>
</body></html>